﻿using System;
using System.Xml;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace OnTheWay.Tool
{
    public class AppConfigHelper
    {
        #region 属性字段
        /// <summary>
        /// xml文档对象
        /// </summary>
        protected XmlDocument mSystemXmlDocument = new XmlDocument();
        /// <summary>
        /// 系统配置 的 终端
        /// </summary>
        protected XmlNodeList mSystemEndpointNodes = null;
        /// <summary>
        /// 文件路径
        /// </summary>
        protected String mMainUIPath = "HuiZhongOA.exe.config";
        /// <summary>
        /// 系统配置 的 终端
        /// </summary>
        protected string mSystemErrorMessage = null;

        #endregion

        #region 构造方法

        public AppConfigHelper()
        {
            this.InitXmlDocument();
        }

        public AppConfigHelper(string mainUIPath)
        {
            mMainUIPath = mainUIPath ;
            this.InitXmlDocument();
        }

        /// <summary>
        /// 通用文档初始化
        /// </summary>
        protected void InitXmlDocument()
        {
            try
            {
                mSystemXmlDocument.Load(mMainUIPath);
                mSystemEndpointNodes = mSystemXmlDocument.GetElementsByTagName("endpoint");
            }
            catch (Exception ex)
            {
                mSystemErrorMessage = ex.Message;
                mSystemXmlDocument = null;
                mSystemEndpointNodes = null;
            }
        }
        
        #endregion

        #region 开放接口

        /// <summary>
        /// 设置服务器地址和端口
        /// 出现错误 函数会抛出错误 请使用try catch
        /// </summary>
        public void SetSystemServerLink(string ip, int port)
        {
            if (mSystemEndpointNodes != null)
            {
                foreach (XmlNode tEndPoint in mSystemEndpointNodes)
                {
                    try
                    {
                        string address = tEndPoint.Attributes["address"].Value.Trim();
                        if (address.IndexOf("Service") > 0)
                        {
                            tEndPoint.Attributes["address"].Value = "http://" + ip + ":" + port + address.Substring(address.IndexOf('/', 7));
                            break;
                        }
                    }
                    catch { }
                }
                mSystemXmlDocument.Save(mMainUIPath);
            }
            else
            {
                throw new Exception("加载配置文件错误：" + mSystemErrorMessage);
            }
        }

        /// <summary>
        /// 设置服务器地址和端口
        /// 出现错误 函数会抛出错误 请使用try catch
        /// </summary>
        public void GetSystemServerLink(ref string ip, ref int port)
        {
            if (mSystemEndpointNodes != null)
            {
                foreach (XmlNode tEndPoint in mSystemEndpointNodes)
                {
                    try
                    {
                        string address = tEndPoint.Attributes["address"].Value.Trim();
                        if (address.IndexOf("Service") > 0)
                        {
                            address = address.Substring(7, address.IndexOf('/', 7) - 7);
                            string[] endpoint = address.Split(':');
                            ip = endpoint[0];
                            port = int.Parse(endpoint[1]);
                            break;
                        }
                    }
                    catch { }
                }
            }
        }

        #endregion
    }
}
